package com.neeraj.SharingArtifacts;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Environment;
import android.util.Log;

public class UploadData{
	
	private static final String DEBUG_TAG = "UPLOAD DATA";
	private static final String IP_ADDRESS = "192.168.1.67";
//	private static final String IP_ADDRESS = "pba.cs.clemson.edu/~neerajj";
	
	File file;
	File mediaStorageDir=null;
	
	public UploadData() {
		Log.d(DEBUG_TAG, "IN constructor!!");		
	}		
	
	boolean doFileUpload1(String path, ArtifactAttributes art){
		       
        MultipartEntity reqEntity = new MultipartEntity();
        boolean status=false;
		if(!art.getArtifactType().equals("text")) {
			String filename = art.getContent_file_name();
			
			if(art.getArtifactType().equals("image")){
				mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
			              Environment.DIRECTORY_DCIM), "SharingArtifacts/image");
			}
			else if(art.getArtifactType().equals("audio")) {
				mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
			              Environment.DIRECTORY_DCIM), "SharingArtifacts/audio");
			}
			else if(art.getArtifactType().equals("video")) {
				mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
			              Environment.DIRECTORY_DCIM), "SharingArtifacts/video");
			}
			file = new File(mediaStorageDir.getPath() + File.separator + filename);
			Log.d(DEBUG_TAG, mediaStorageDir.getPath() + File.separator + filename);
			if(file.exists()) {
				Log.d(DEBUG_TAG, "File exists");
				FileBody bin1 = new FileBody(file);
				reqEntity.addPart("file", bin1);
			}
		} else {
			try {
				Log.i(DEBUG_TAG, "Uploading Text: " + art.getContent());
				reqEntity.addPart("content", new StringBody(art.getContent()));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try{
//			String urlString = "http://172.22.125.2/insertData.php";
//			String urlString = "http://pba.cs.clemson.edu/~neerajj/insertData1.php";
//			String urlString = "http://192.168.1.67/insertData.php";
			String urlString = "http://"+ IP_ADDRESS +"/insertData.php";
			
			HttpClient client = new DefaultHttpClient();
	    	HttpPost post = new HttpPost(urlString);
	    	reqEntity.addPart("artifactType", new StringBody(art.getArtifactType()));
			reqEntity.addPart("latitude", new StringBody(Double.toString(art.getLatitude())));
			reqEntity.addPart("longitude", new StringBody(Double.toString(art.getLongitude())));
			reqEntity.addPart("altitude", new StringBody(Double.toString(art.getAltitude())));
			reqEntity.addPart("utc_time", new StringBody(Long.toString(art.getUtcTime())));
			reqEntity.addPart("date", new StringBody(art.getDate()));
			reqEntity.addPart("filename", new StringBody(art.getContent_file_name()));			
			post.setEntity(reqEntity);
			HttpResponse response = client.execute(post);
			BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			String line = null;
			while((line = in.readLine()) != null) {
				Log.i(DEBUG_TAG, line);				
				if(line.contains("done")){
					Log.i(DEBUG_TAG, "Helloooooooooooooooooooooooo");					
					status = true;
					break;
				}				
			}
//			if(status) {
//				File delFile = file;
//				boolean isFileDel = delFile.delete();
//				Log.i(DEBUG_TAG, "file deletion status:"+ isFileDel);
//			}
		} catch (Exception ex){
			Log.e(DEBUG_TAG, "error: " + ex.getMessage(), ex);
		}
		return status;
	}	 		
}
